#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTable.Input.MultiTouch;
using System.Collections.ObjectModel;
using UTable.Objects.Controls;
using System.Windows;

namespace UTable.Objects.Handlers
{
    public class MultiFingerGestureHandler : CertainFingerGestureHandler
    {
        #region Private Fields

        UIElement _relativeElement;

        #endregion

        #region Properties

        public Dictionary<int, CertainFingerGestureHandler> GestureHandlers = new Dictionary<int, CertainFingerGestureHandler>();

        #endregion

        #region Constructors

        public MultiFingerGestureHandler(IControl node, UIElement relativeElement)
			: base(node)
		{
			_relativeElement = relativeElement;
		}

		#endregion

        #region Protected Methods

        protected override void OnContactDown(FingerEventArgs args)
        {
            if (GestureHandlers.ContainsKey(CurrentFingers.Count))
            {
                GestureHandlers[CurrentFingers.Count].OnCertainFingersCancelled(CurrentFingers);
            }

            if (CaptureFingers)
                Node.CaptureFinger(args.FingerID);
            CurrentFingers.Add(args);
            args.Handled = true;
            if (GestureHandlers.ContainsKey(CurrentFingers.Count))
                GestureHandlers[CurrentFingers.Count].OnCertainFingersDown(CurrentFingers);
        }

        protected override void OnContactMove(FingerEventArgs args)
        {
            args.Handled = true;
            UpdateFingerStatus(args);

            if (GestureHandlers.ContainsKey(CurrentFingers.Count))
            {
                GestureHandlers[CurrentFingers.Count].OnCertainFingersMove(CurrentFingers);
            }
        }

        protected override void OnContactUp(FingerEventArgs args)
        {
            UpdateFingerStatus(args);
            if (CaptureFingers)
                Node.UncaptureFinger(args.FingerID);
            args.Handled = true;

            if (GestureHandlers.ContainsKey(CurrentFingers.Count))
            {
                GestureHandlers[CurrentFingers.Count].OnCertainFingersUp(CurrentFingers);
            }

            RemoveFinger(args.FingerID);
            if (GestureHandlers.ContainsKey(CurrentFingers.Count))
            {
                GestureHandlers[CurrentFingers.Count].OnCertainFingersDown(CurrentFingers);
            }
        }
        #endregion


        public override int FingerNumber
        {
            get { return int.MaxValue; }
        }

        public override void OnCertainFingersDown(Collection<FingerEventArgs> fingers)
        {
            // do nothing
        }

        public override void OnCertainFingersMove(Collection<FingerEventArgs> fingers)
        {
            // do nothing
        }

        public override void OnCertainFingersUp(Collection<FingerEventArgs> fingers)
        {
            // do nothing
        }

        public override void OnCertainFingersCancelled(Collection<FingerEventArgs> fingers)
        {
            // do nothing
        }
    }
}
